/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJO.HocSinh_LopPOJO;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * @author
 *  ITEC.HCMUS.EDU.VN
 *  Ngô Huy Đạt
 *  0869005
 *  nhdat.itec@gmail.com
 */
public class HocSinh_LopDAO {

    public static ArrayList<HocSinh_LopPOJO> selectHocSinh_Nam(String MaNamHoc){
        ArrayList<HocSinh_LopPOJO> list = new ArrayList<HocSinh_LopPOJO>();
        try{
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = "SELECT hs.MaHocSinh, TenHocSinh, l.MaLop "
                    + "FROM hocsinh hs JOIN hocsinh_lop hsl ON hs.MaHocSinh = hsl.MaHocSinh "
                    + "             JOIN dslop l ON hsl.MaLop = l.MaLop "
                    + "WHERE MaNamHoc = '"+MaNamHoc+"' "
                    + "ORDER BY MaLop";
            ResultSet rs = helper.excecuteQuery(sql);
            while(rs.next()){
                HocSinh_LopPOJO hsl = new HocSinh_LopPOJO();
                hsl.setMaHocSinh(rs.getString("MaHocSinh"));
                hsl.setHocSinh(rs.getString("TenHocSinh"));
                hsl.setLop(rs.getString("MaLop"));
                list.add(hsl);
            }

            helper.close();
        }catch(Exception ex){
            System.out.print(ex.getMessage());
        }
        return list;
    }

    public static boolean insert(String MaHocSinh,String MaLop){
        boolean kq = false;
        try{
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = String.format("INSERT INTO hocsinh_lop(MaHocSinh,MaLop) VALUES('%s','%s')",MaHocSinh,MaLop);
            int num = helper.excecuteUpdate(sql);
            if(num==1)  kq = true;

            helper.close();
        }catch(Exception ex){
            System.out.print(ex.getMessage());
        }
        return kq;
    }

    public static boolean  delete(String MaHocSinh,String MaLop) {
        boolean kq = false;
        try {
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();

            String sql = String.format("DELETE FROM hocsinh_lop WHERE MaHocSinh = '%s' AND MaLop = '%s' ",MaHocSinh,MaLop);
            int num = helper.excecuteUpdate(sql);
            if(num==1)  kq = true;

            helper.close();
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
        return kq;
    }
}
